package cn.ekuma.epos.datalogic.define.dao.sale;

import java.util.List;

import com.openbravo.data.loader.query.QBFParameters;
import e.odbo.data.dao.ViewDAO;
import com.openbravo.bean.sales.TicketsInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.sqlbuilder.QBFBuilder;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;

public class TicketsInfoDAO extends ViewDAO<TicketsInfo>{

    public TicketsInfoDAO(I_Session s) {
        super(s);
    }

    public TicketsInfo readValues(DataRead dr,TicketsInfo obj) throws BasicException {
        if(obj==null)
        	obj=new TicketsInfo();
        obj.setM_sId(dr.getString(1));
        obj.setTicketid(dr.getInt(2));
        obj.setTickettype(dr.getInt(3));
        obj.setDate(dr.getTimestamp(4));
        obj.setName(dr.getString(5));
        obj.setCustomer( dr.getString(6));
        obj.setTotalPrice(dr.getDouble(7));
        obj.setTotalTax(dr.getDouble(8));
        obj.setTotalConsultCost(dr.getDouble(9));
        obj.setM_dPriceRate(dr.getDouble(10));
        obj.setHostName(dr.getString(11));
        obj.setLocation(dr.getString(12));
        return obj;
    }

    public List<TicketsInfo> list(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT T.ID,T.TICKETID, T.TICKETTYPE, T.CURDATE, P.NAME, C.NAME, T.TOTALPRICE,T.TOTALTAX,T.TOTALCONSULTCOST,T.PRICERATE,T.HOSTNAME,S.NAME "
                + "FROM TICKETS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID LEFT OUTER JOIN LOCATIONS S ON  T.LOCATIONREF=S.ID "
                + "WHERE ?(QBF_FILTER) ORDER BY T.CURDATE DESC, T.TICKETID",
                new String[]{"T.CURDATE", "T.CURDATE", "C.ID","S.ID", "P.ID"}),
                new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}),
                this).list(filter);
    }

    @Override
    public List<TicketsInfo> query(QBFParameters qbfParameters) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT T.ID,T.TICKETID, T.TICKETTYPE, T.CURDATE, P.NAME, C.NAME, T.TOTALPRICE,T.TOTALTAX,T.TOTALCONSULTCOST,T.PRICERATE,T.HOSTNAME,S.NAME "
                        + "FROM TICKETS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID LEFT OUTER JOIN LOCATIONS S ON  T.LOCATIONREF=S.ID "
                        + "WHERE ?(QBF_FILTER) ORDER BY T.CURDATE DESC, T.TICKETID"),
               null,
                this).list(qbfParameters);
    }

    @Override
    public Class getSuportClass() {
        return TicketsInfo.class;
    }
    
    public List<TicketsInfo> listByTicketId(String id) throws BasicException {
    	return new PreparedSentence(s
                , "SELECT T.ID,T.TICKETID, T.TICKETTYPE, T.CURDATE, P.NAME, C.NAME, T.TOTALPRICE,T.TOTALTAX,T.TOTALCONSULTCOST,T.PRICERATE,T.HOSTNAME,S.NAME "
                + "FROM TICKETS T LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID LEFT OUTER JOIN LOCATIONS S ON  T.LOCATIONREF=S.ID "
                + "WHERE  T.TICKETID=? "
                ,SerializerWriteString.INSTANCE ,this).list(id);
    }
    
}
